<template>
  <el-card shadow="always" style="width: 95%">
<!--    1. 表单查询-->
    <el-form :inline="true" :model="studSearch" class="demo-form-inline" size="medium">
      {{studSearch}}
      <el-form-item >
        <el-input v-model="studSearch.sname" placeholder="学生姓名"></el-input>
      </el-form-item>
      <el-form-item >
        <el-input v-model="studSearch.addr" placeholder="学生住址"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="studSearch.cid" placeholder="所在班级">
          <el-option label="所有班级" value="0"></el-option>
          <el-option :label="c.cname" :value="c.cid" v-for="c in classes"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
            :data="students"
            style="width: 100%">
      <el-table-column
              label="编号"
              prop="sid"
              width="180"/>
      <el-table-column
              label="姓名"
              prop="sname"
              align="center"/>
      <el-table-column
              label="性别"
              prop="sex"
              align="center"/>
      <el-table-column
              label="年龄"
              prop="age"
              align="center"/>
      <el-table-column
              label="住址"
              prop="addr"
              align="center"/>
      <el-table-column
              label="所在班级"
              prop="cname"
              align="center"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
                  size="mini"
                  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
<!--    分页导航-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
  </el-card>

</template>

<script>
  export default {
    data() {
      return {
        students: [],
        page:1,       // 当前页
        pageSize:5,   // 每页大小
        total:100,    // 总记录数
        studSearch:{}, // 代表要搜索学生的条件
        classes:[],    // 班级集合
      }
    },
    created() {
      //1. 加载分页(不带查询条件)
      // this.findByPage()
      //2. 查询所有班级
      this.findClasses()
      //3. 条件查询带分页
      this.search()
    },
    methods: {
      //0. 条件查询带分页
      search(){
        this.$axios.post(`/student/search.do?page=${this.page}&pageSize=${this.pageSize}`,this.studSearch)
                .then(({data}) => {
                  this.students = data.data.rows    // data.data: 相当于：pageResult对象
                  this.total = data.data.total
                })
      },
      //1. 查询所有班级
      findClasses(){
        this.$axios.get("/classes/list.do").then(({data}) => {
          this.classes = data.data
        })
      },
      //2. 分页查询学生列表
      findByPage(){
        this.$axios.get(`/student/findByPage.do?page=${this.page}&pageSize=${this.pageSize}`)
                .then(({data}) => {
            this.students = data.data.rows    // data.data: 相当于：pageResult对象
            this.total = data.data.total
        })
      },
      // pageSize大小改变时触发
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val
        this.search()
      },
      // page大小改变时触发
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.page = val
        this.search()
      },
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
        console.log(index, row);
      }
    }
  }
</script>
<style>
  .el-card{
    margin: 0 auto;
  }
  .el-pagination{
    text-align: right;
    margin-top: 20px;
  }
</style>